package utils

import (
	"unicode/utf8"
)

func MbStrLeg(s string) int {
	return utf8.RuneCountInString(s)
}

func MbSubStr(s string, start int, a ...int) string {

	if s == "" {
		return s
	}

	var p, length int

	mbtab := []int{}
	for p = range s {
		mbtab = append(mbtab, p)
	}

	mbtablen := len(mbtab)

	if start < 0 {
		start = mbtablen + start
	}
	if start < 0 {
		start = 0
	}
	if len(a) > 0 {
		length = a[0]
	} else {
		length = mbtablen
	}

	if length < 0 {
		length = mbtablen - start + length
	}

	if length < 0 {
		return ""
	}

	if start > mbtablen {
		return ""
	}

	if length+start >= mbtablen {
		return s[mbtab[start]:]
	} else {
		return s[mbtab[start]:mbtab[length+start]]
	}
}

func UcFirst(s string) string {
	if len(s) < 1 {
		return ""
	}

	str := []rune(s)

	if str[0] >= 97 && str[0] <= 122 {
		str[0] -= 32
	}
	return string(str)
}

func LcFirst(s string) string {
	if len(s) < 1 {
		return ""
	}

	str := []rune(s)
	if str[0] >= 65 && str[0] <= 90 {
		str[0] += 32
	}
	return string(str)
}
